<?php

/* vim: set expandtab tabstop=4 shiftwidth=4: */

// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

Class AdminAction extends backendAction{

    var $members;
    var $Fuc;
    var $category_image_path = "upload/category_image/";
    var $Api;

    public function _initialize() {
        $this->Api = A("Api");
        import("@.ORG.UpConf");
        $this->Fuc = A("Member");
        $this->members[username] = $_SESSION['username'];
        $this->members[uid] = $_SESSION['uid'];
        $this->members[purview] = $_SESSION['purview'];
        // print_r($members);exit();
        if (!empty($this->members[username]) && ($this->members[purview] == 1)) {
            $this->assign('Example_username', $this->members[username]);
            $this->assign('Example_uid', $this->members[uid]);
        } else {
            $this->redirect_ext();
        }
    }

    public function index() {
        $this->Fuc->navigation('会员管理');

        $kros = M("members"); // 实例化模型类
        import("@.ORG.Page"); //导入分页类
        $count = $kros->count(); //计算总数
        $p = new Page($count, 10);
        $list = $kros->limit($p->firstRow . ',' . $p->listRows)->order('uid desc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);

  

        $this->assign('list', $list);
        $this->display();
    }

    public function room() {
        $this->Fuc->navigation('直播间管理');

        $kros = M("members"); // 实例化模型类
        import("@.ORG.Page"); //导入分页类
        $count = $kros->count(); //计算总数
        $p = new Page($count, 10);
        $list = $kros->limit($p->firstRow . ',' . $p->listRows)->order('uid desc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);



        $this->assign('list', $list);
        $this->display();
    }

    public function gift() {
        $this->Fuc->navigation('礼物管理');
        $list = D('gift')->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function update_gift() {
        $this->display("block:update_gift");
    }

    public function update_gift_act() {
        $gift_id = $_GET[gift_id];
        $gift_name = $_GET[gift_name];
        $price = $_GET[price];
        $score = $_GET[score];

        $map = array(
            'gift_id' => $gift_id,
        );
        $data = array(
            'gift_name' => $gift_name,
            'price' => $price,
            'score' => $score,
        );
        if ($gift_id && $gift_name && $price && $score) {
            $res = D('gift')->where($map)->save($data);
        }

        $res ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
    }

    public function category() {
        $this->Fuc->navigation('分类列表');
        $kros = M("category"); // 实例化模型类
        import("@.ORG.Page"); //导入分页类
        $count = $kros->count(); //计算总数
        $p = new Page($count, 10);
        $list = $kros->limit($p->firstRow . ',' . $p->listRows)->order('cate_id desc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);
        //$list =  $kros ->order('id desc')->select(); // 创建数据对象
        //var_dump($list);
        //$this->assign('sss',$sss);
        $this->assign('list', $list);
        $this->display();
    }

    public function category_add() {
        $this->Fuc->navigation('添加分类');

        $this->assign('act', "add");


        $this->display("category_edit");
    }

    public function category_edit() {
        $this->Fuc->navigation('编辑');

        $cate_id = $_GET['cate_id'];
        $this->assign('cate_id', $cate_id);
        $this->assign('act', "edit");
        $this->display("category_edit");
    }

    public function live_address_edit() {
        
         $a = C('live_server_address');
        $this->assign('live_server_address', $a);
        $this->Fuc->navigation('直播服务器地址修改');


        $this->assign('act', "edit");
        $this->display();
    }

    public function category_delete() {
        $this->Fuc->navigation('删除');

        $cate_id = $_GET['cate_id'];
        $map['cate_id'] = $cate_id;

        M("category")->where($map)->delete();

        $this->redirect(U('/Admin/category'), array(), 5, ' 操作成功!,5秒钟后页面跳转中 ~');
    }

    public function member_delete() {


        $uid = $_GET['uid'];
        $map['uid'] = $uid;

        M("members")->where($map)->delete();

        $this->redirect(U('/Admin/'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function category_edit_act() {

        $act = $_POST[act];

        $fileName = $_FILES["file"]["name"];

        $cate_name = $_POST[cate_name];


        $sort_order = $_POST[sort_order];

        $kuozhan = explode(".", $fileName);


        $newname = date('YmdHis') . rand(100, 999) . "." . $kuozhan[1];

        //print_r($newname);exit();

        move_uploaded_file($_FILES["file"]["tmp_name"], $this->category_image_path . $newname);

        $data = array(
            'cate_name' => $cate_name,
            'sort_order' => $sort_order,
            'image' => $newname,
        );

        switch ($act) {
            case 'add':

                $res = M("category")->add($data);
                break;



            default:

                $map['cate_id'] = $_POST[cate_id];

                M("category")->where($map)->save($data);



                break;
        }


        $this->redirect(U('/Admin/category'), array(), 5, ' 操作成功!,5秒钟后页面跳转中 ~');
    }

    public function member_edit_act() {


        $purview = $_GET[v];

        $data = array(
            'purview' => $purview,
        );


        $map['uid'] = $_GET[uid];

        M("members")->where($map)->save($data);




        $this->redirect(U('/Admin/index'), array(), 3, ' 操作成功!,3秒钟后页面跳转中 ~');
    }

    public function room_edit_act() {


        $reviewed = $_GET[v];

        $data = array(
            'reviewed' => $reviewed,
        );


        $map['uid'] = $_GET[uid];

        M("members")->where($map)->save($data);




        $this->redirect(U('/Admin/room'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function redirect_ext() {
        $this->redirect(U('/Adminlogin/'), array(), 3, ' 您尚未登录!,3秒钟后页面跳转中 ~');
    }

    public function live_server_address() {


        $this->Fuc->navigation('直播服务器地址设置');

        $a = C('live_server_address');
        $this->assign('live_server_address', $a);
        $this->display();
    }

    public function chat_server_address() {

        $a = C('live_server_address');
        $this->assign('live_server_address', $a);
        
        $this->Fuc->navigation('弹幕服务器地址设置');

        $a = C('chat_server_address');
        $this->assign('chat_server_address', $a);
        $this->display();
    }
public function chat_address_edit() {
  
        $a = C('chat_server_address');
        $this->assign('chat_server_address', $a);
         $this->display();
        
}  


      
        
        
    public function live_server_address_edit() {


        $live_address = $_POST['live_address'];

         $arr['live_server_address'] =$live_address;


        $res= $this->update_config($arr, './Conf/config.php');


        $this->redirect(U('/Admin/live_server_address'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function chat_server_address_edit() {

     
        
        $chat_address = $_POST['chat_address'];

        $arr['chat_server_address'] = $chat_address;


        $res= $this->update_config($arr, './Conf/config.php');


        $this->redirect(U('/Admin/chat_server_address'), array(), 2, ' 操作成功!,2秒钟后页面跳转中 ~');
    }

    public function gains_clearing() {

        if ($_GET['time_start']) {

            $time_start = strtotime($_GET['time_start']);

            $time_end = strtotime($_GET['time_end']);

            $condition['act_time'] = array(array('egt', $time_start), array('elt', $time_end));

            // $condition[to_id] = $_SESSION['uid'];

            $ret = D('charge_log')->where($condition)->findAll();

            $this->assign('ret', $ret);
            $this->assign('sum', $sum);
            $this->assign('start_time', $time_start);
            $this->assign('end_time', $time_end);
            // return print_r($ret);
        }
        $this->display();
    }

    public function gains_clearing_act() {

        if ($_SESSION['uid']) {

            $id = ($_GET['id']);

            $condition[id] = $id;
            $condition[type] = 1;

            $data = array(
                'type' => 2,
            );

            $res = M('charge_log')->where($condition)->save($data);


            $map[id] = $id;

            $list = D("charge_log")->where($map)->find();

            unset($map);

            $uid = $list[uid];


            $start_id = $list['start_id'];

            $end_id = $list['end_id'];

            $data = array(
                'type' => 2,
            );

            $map[to_id] = 1;

            $map['id'] = array(array('egt', $start_id), array('elt', $end_id));

            $info = M('gift_log')->where($map)->save($data);

            //echo $res.$info;
            ($info && $res) ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
        }
    }

    public function gains_list() {
        $this->Fuc->navigation('主播收益明细表');
        if ($_GET['time_start']) {

            $time_start = strtotime($_GET['time_start']);

            $time_end = strtotime($_GET['time_end']);
            $map['give_time'] = array(array('egt', $time_start), array('elt', $time_end));
        }
        $kros = M("gift_log"); // 实例化模型类
        import("@.ORG.Page"); //导入分页类
        $count = $kros->where($map)->count(); //计算总数
        $p = new Page($count, 15);
        $list = $kros->where($map)->limit($p->firstRow . ',' . $p->listRows)->order('id desc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);

        $this->assign('list', $list);
        $this->display();
    }

    public function grade() {
        $this->Fuc->navigation('等级管理');
        $kros = M("grade"); // 实例化模型类
        import("@.ORG.Page"); //导入分页类
        $count = $kros->count(); //计算总数
        $p = new Page($count, 15);
        $list = $kros->limit($p->firstRow . ',' . $p->listRows)->order('id asc')->findAll();
        $p->setConfig('header', '篇记录');
        $p->setConfig('prev', "上一页");
        $p->setConfig('next', '下一页');
        $p->setConfig('first', '<<');
        $p->setConfig('last', '>>');
        $page = $p->show();
        $this->assign("page", $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function update_grade() {
        $this->display("block:update_grade");
    }
    
     public function send_message() {
        $this->display("block:send_message");
    }

    public function update_grade_act() {
        $id = $_GET[id];
        $act = $_GET[act];
        $grade_level = $_GET[grade_level];
        $need_score = $_GET[need_score];
        $grade_name = $_GET[grade_name];
        $map = array(
            'id' => $id,
        );
        $data = array(
            'grade_level' => $grade_level,
            'need_score' => $need_score,
            'grade_name' => $grade_name,
        );
        if ($act == "add") {

            $res = D('grade')->add($data);
            $res ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
        }
        if ($id && $grade_level && $need_score && $grade_name) {
            $res = D('grade')->where($map)->save($data);
        }

        $res ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
    }
    
     public function send_message_act() {
        $act = $_GET[act];
        $title = $_GET[title];
        $content = $_GET[content];
        $data = array(
            'content' => $content,
            'title' => $title,
            'time'    => date('Y-m-d H:i:s')
        );
        if ($act == "add") {

            $id = D('message')->add($data);
           
            
            
             $kros = D("members"); // 实例化模型类
             
        $sql = "UPDATE `z_members` SET message_ids = CONCAT( IFNULL(message_ids,'') ,'{$id},')";
        
        $res = $kros->query($sql);
        
        
        }
        
      ($res !== false )? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");

    }
    
    public function review_act() {
        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];

        if (!empty($members[username])) {
                             $data = array(
                                'review' => 2,
                            );
            $condition[uid]=$_GET[uid];
            $res = M('members')->where($condition)->save($data);            
            
        }
        
       $res ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
 }

  public function about_us() {
        $this->Fuc->navigation('关于我们');
        $about_us = C('ABOUT_US');
        $this->assign('about_us', $about_us);
        $this->display();
        
    }
    
      public function about_us_edit() {
        $members[username] = $_SESSION['username'];
        $uid=$members[uid] = $_SESSION['uid'];
        $content=trim($_GET[content]);
        if (!empty($members[username])) {
        
         $arr['ABOUT_US'] =  $content;


        $res= $this->update_config($arr, './Conf/config.php');
                             
              
        $res ? $this->Api->successres("操作成功") : $this->Api->faileres("操作失败!");
        return;
        }
        
        $this->Api->faileres("操作失败!");
        
    }
    
    
}
?>    
